package com.xueyi.common.core.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author gaorongyi
 */
public class XueyiStringUtil extends StringUtils implements StringPool {
    private XueyiStringUtil() {
    }

    public static String getLike(String name, boolean left, boolean right) {
        if (null == name) {
            return null;
        } else {
            StringBuilder stringBuilder = new StringBuilder();
            if (left) {
                stringBuilder.append(StringPool.PERCENT);
            }
            stringBuilder.append(name);
            if (right) {
                stringBuilder.append(StringPool.PERCENT);
            }
            return stringBuilder.toString();
        }
    }

    public static String getString(Object o) {
        if (null == o) {
            return "";
        } else {
            return o.toString();
        }
    }

    public static String getNumString(Object o) {
        if (null == o) {
            return StringPool.NUMBER_0;
        } else {
            return o.toString();
        }
    }


    public static boolean containsChinese(String s) {
        if (isEmpty(s)) {
            return false;
        }
        for (int i = 0; i < s.length(); i++) {
            if (isChinese(s.charAt(i))) {
                return true;
            }
        }
        return false;
    }

    public static boolean isChinese(char a) {
        int v = a;
        return (v >= 19968 && v <= 171941);
    }

    /**
     * 用冒号拼装String
     *
     * @param strs 拼装字符串
     * @return 返回使用冒号组合后的String字符串
     */
    public static String combineStringColon(Object... strs) {
        return combineStringDefined(StringPool.COLON, strs);
    }

    /**
     * 用下划线拼装String
     *
     * @param strs 拼装字符串
     * @return 返回使用下划线组合后的String字符串
     */
    public static String combineStringUnderline(Object... strs) {
        return combineStringDefined(StringPool.UNDERLINE, strs);
    }

    /**
     * 用指定分隔符拼装String
     *
     * @param combineStr 指定分隔符
     * @param strs       拼装字符串
     * @return result
     */
    public static String combineStringDefined(String combineStr, Object... strs) {
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < strs.length; i++) {
            result.append(strs[i] == null ? null : strs[i].toString());
            if (i < strs.length - 1) {
                result.append(combineStr);
            }
        }
        return result.toString();
    }

    /**
     * 含有unicode 的字符串转一般字符串
     *
     * @param unicodeStr 混有 Unicode 的字符串
     * @return
     */
    public static String unicodeStr2String(String unicodeStr) {
        int length = unicodeStr.length();
        int count = 0;
        //正则匹配条件，可匹配“\\u”1到4位，一般是4位可直接使用 String regex = "\\\\u[a-f0-9A-F]{4}";
        String regex = "\\\\u[a-f0-9A-F]{1,4}";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(unicodeStr);
        StringBuffer sb = new StringBuffer();

        while (matcher.find()) {
            //原本的Unicode字符
            String oldChar = matcher.group();
            //转换为普通字符
            String newChar = unicode2String(oldChar);
            // 在遇见重复出现的unicode代码的时候会造成从源字符串获取非unicode编码字符的时候截取索引越界等
            int index = matcher.start();
            //添加前面不是unicode的字符
            sb.append(unicodeStr, count, index);
            //添加转换后的字符
            sb.append(newChar);
            //统计下标移动的位置
            count = index + oldChar.length();
        }
        //添加末尾不是Unicode的字符
        sb.append(unicodeStr, count, length);
        return sb.toString();
    }

    /**
     * 字符串转换unicode
     *
     * @param string
     * @return
     */
    public static String string2Unicode(String string) {
        StringBuffer unicode = new StringBuffer();
        for (int i = 0; i < string.length(); i++) {
            // 取出每一个字符
            char c = string.charAt(i);
            // 转换为unicode
            unicode.append("\\u" + Integer.toHexString(c));
        }

        return unicode.toString();
    }

    /**
     * unicode 转字符串
     *
     * @param unicode 全为 Unicode 的字符串
     * @return
     */
    public static String unicode2String(String unicode) {
        StringBuffer string = new StringBuffer();
        String[] hex = unicode.split("\\\\u");

        for (int i = 1; i < hex.length; i++) {
            // 转换出每一个代码点
            int data = Integer.parseInt(hex[i], 16);
            // 追加成string
            string.append((char) data);
        }

        return string.toString();
    }

}